package com.sgzn.sgpay.web.controller;

import com.sgzn.sgpay.model.request.SgsMainOrderQueryPageRequest;
import com.sgzn.sgpay.model.request.util.PageQueryParam;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.MainOrderQueryPageResult;
import com.sgzn.sgpay.service.SgsMainOrderService;
import com.sgzn.sgpay.web.controller.base.MerchantBaseController;
import com.sgzn.sgpay.web.handle.AssembleCommonHandle;
import com.sgzn.sgpay.web.request.Impl.MainOrderQueryPageRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 主订单管理控制器
 * @author huangsenguang
 * @version Id: MainOrderController.java, v 0.1 2018/5/1 11:08 huangsenguang Exp $$
 */
@Slf4j
@Controller
@RequestMapping("/merchant")
public class MainOrderController extends MerchantBaseController {
    @Autowired
    private SgsMainOrderService sgsMainOrderService;
    @Resource
    private AssembleCommonHandle assembleCommonHandle;

    /**
     * 分页查询订单的信息
     * @param request
     * @return
     */
    @RequestMapping("/mainorder/queryPage")
    @ResponseBody
    public SgsResultResponse<MainOrderQueryPageResult> queryPageMainOrder(MainOrderQueryPageRequest request) {
        super.setMchInfo(request);
        return this.sgsMainOrderService.queryPage(assembleQueryPageRequest(request));
    }

    /**
     * 组装服务请求
     * @param request
     * @return
     */
    private SgsMainOrderQueryPageRequest assembleQueryPageRequest(MainOrderQueryPageRequest request) {
        SgsMainOrderQueryPageRequest sgsMainOrderQueryPageRequest = new SgsMainOrderQueryPageRequest();
        this.assembleCommonHandle.assemble(request, sgsMainOrderQueryPageRequest);
        sgsMainOrderQueryPageRequest.setOrderId(request.getOrderId());
        sgsMainOrderQueryPageRequest.setOrderType(request.getOrderType());
        sgsMainOrderQueryPageRequest.setOrderState(request.getOrderState());
        PageQueryParam pageQueryParam = new PageQueryParam();
        pageQueryParam.setPageSize(Integer.parseInt(request.getPageSize()));
        pageQueryParam.setCurrentPage(Integer.valueOf(request.getPageNum()));
        sgsMainOrderQueryPageRequest.setPageQueryParam(pageQueryParam);
        return sgsMainOrderQueryPageRequest;
    }
}

